package com.joolun.web.controller.mall;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.common.utils.SecurityUtils;
import com.joolun.common.utils.StringUtils;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.mall.domain.vo.ChainPlanReserveOrderError;
import com.joolun.mall.domain.vo.ChainTbWdtRelError;
import com.joolun.mall.entity.Brand;
import com.joolun.mall.entity.ChainTbWdtRel;
import com.joolun.mall.service.IBrandService;
import com.joolun.mall.service.IChainTbWdtRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 管理端-淘宝旺店通关系
 *
 * @author www.joolun.com
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/tbWdtRel")
public class ChainTbWdtRelController {

    @Autowired
    private IChainTbWdtRelService tbWdtRelService;

    @Autowired
    private IBrandService brandService;

    /**
     * 淘宝旺店通关系-分页
     * @param page
     * @param query
     * @return
     */
    @Log(title = "淘宝旺店通关系-分页", businessType = BusinessType.QUERY)
    @GetMapping("/page")
    public AjaxResult page(Page<ChainTbWdtRel> page, ChainTbWdtRel query)
    {
        Page<ChainTbWdtRel> page1 = getChainTbWdtRelPage(page, query);
        return AjaxResult.success(page1);
    }

    private Page<ChainTbWdtRel> getChainTbWdtRelPage(Page<ChainTbWdtRel> page, ChainTbWdtRel query) {
        Page<ChainTbWdtRel> page1 = tbWdtRelService.page(page,
                Wrappers.<ChainTbWdtRel>lambdaQuery()
                        .in(ChainTbWdtRel::getDelFlag, "0")
                        .in(CollectionUtil.isNotEmpty(query.getBrandIds()),ChainTbWdtRel::getBrandId, query.getBrandIds())
                        .in(CollectionUtil.isNotEmpty(query.getVipTypes()),ChainTbWdtRel::getVipType, query.getVipTypes())
                        .like(StringUtils.isNotBlank(query.getTbUserId()), ChainTbWdtRel::getTbUserId, query.getTbUserId())
                        .like(StringUtils.isNotBlank(query.getWdtEncodeUserId()), ChainTbWdtRel::getWdtEncodeUserId, query.getWdtEncodeUserId())
                        .like(StringUtils.isNotBlank(query.getWxNickName()), ChainTbWdtRel::getWxNickName, query.getWxNickName())
                        .like(StringUtils.isNotBlank(query.getPhone()), ChainTbWdtRel::getPhone, query.getPhone())
                        .like(StringUtils.isNotBlank(query.getRemark()), ChainTbWdtRel::getRemark, query.getRemark())
                        .orderByDesc(ChainTbWdtRel::getId)

        );
        return page1;
    }

    /**
     * 淘宝旺店通关系-新增
     * @param dto
     * @return
     */
    @Log(title = "淘宝旺店通关系-新增", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ChainTbWdtRel dto)
    {
        return AjaxResult.success(tbWdtRelService.add(dto));
    }

    /**
     * 淘宝旺店通关系-修改
     * @param dto
     * @return
     */
    @PostMapping("edit")
    @Log(title = "淘宝旺店通关系-修改", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult edit(@RequestBody ChainTbWdtRel dto)
    {
        tbWdtRelService.updateInfo(dto);

        return AjaxResult.success();
    }

    /**
     * 淘宝旺店通关系-删除
     * @param relIds 关系id列表
     * @return
     */
    @Log(title = "淘宝旺店通关系-删除", businessType = BusinessType.DELETE)
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody List<Long> relIds)
    {
        LambdaQueryWrapper<ChainTbWdtRel> queryWrapper = new LambdaQueryWrapper<>();
        ChainTbWdtRel tbWdtRel = new ChainTbWdtRel();
        tbWdtRel.setDelFlag("2");
        tbWdtRel.setUpdateBy(SecurityUtils.getUsername());
        tbWdtRelService.update(tbWdtRel,queryWrapper.in(ChainTbWdtRel::getId, relIds));
        return AjaxResult.success();
    }

    /**
    * 淘宝旺店通关系-导入
     */
    @Log(title = "淘宝旺店通关系-导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ChainTbWdtRel> util = new ExcelUtil<>(ChainTbWdtRel.class);
        List<ChainTbWdtRel> datas = util.importExcel(file.getInputStream());
        List<ChainTbWdtRelError> errorList = tbWdtRelService.importData(datas);
        if (!errorList.isEmpty()){
            //导出数据
            ExcelUtil<ChainTbWdtRelError> util2 = new ExcelUtil<>(ChainTbWdtRelError.class);
            return util2.exportExcel(errorList, "VIP用户清单错误信息");
        }else{
            return AjaxResult.success("导入成功");
        }
    }

    @GetMapping("/export")
    public AjaxResult export(Page<ChainTbWdtRel> page, ChainTbWdtRel query)
    {
        page.setSize(-1);
        Page<ChainTbWdtRel> page1 = getChainTbWdtRelPage(page, query);
        List<ChainTbWdtRel> records = page1.getRecords();
        ExcelUtil<ChainTbWdtRel> util = new ExcelUtil<>(ChainTbWdtRel.class);
        return util.exportExcel(records, "VIP用户清单");
    }
}
